/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controllers;

import com.entities.Company;
import com.entities.Denomination;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author ben
 */
@Stateless
public class DenominationController {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em;
    private static final Logger logger = Logger.getLogger(CompanyController.class.getName());
    private Denomination denomination;

    public Long save(Denomination denomination) {
        try {
            em.persist(denomination);
            System.out.println("Saving Denomination complete");
            return denomination.getId();
        } catch (Exception e) {
            System.out.println("Error saving Denomination " + e.toString());
            return -1L;
        }
    }

    public Denomination findDenomination(Long id) {
        logger.log(Level.INFO, "{0} ** ", id.toString());
        try {
            denomination = em.find(Denomination.class, id);
            logger.log(Level.INFO, "Denomination found :  {0}", denomination.getId().toString());
            return denomination;

        } catch (Exception e) {
            logger.log(Level.SEVERE, "Could not find Denomination");
            e.printStackTrace();
            return null;

        }


    }

    public List<Denomination> getAllDenominations() {
        try {
            Query findDenominationQuery = em.createNamedQuery("Denomination.findAll");
            List<Denomination> denominations = findDenominationQuery.getResultList();
            System.out.println("Found list of Denominations");
            return denominations;

        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }
}
